package com.kj.xuyuan.mine;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.base.ActivityCollector;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.login.OneKeyLoginActivity;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.kj.xuyuan.util.WarningDialog;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置
 */
public class SetActivity extends BaseActivity {

    @BindView(R.id.ll_bind_account)
    LinearLayout llBindAccount;
    @BindView(R.id.ll_black_list)
    LinearLayout llBlackList;
    @BindView(R.id.ll_delete_account)
    LinearLayout llDeleteAccount;
    @BindView(R.id.ll_private_set)
    LinearLayout llPrivateSet;
    @BindView(R.id.ll_check_version)
    LinearLayout llCheckVersion;
    @BindView(R.id.ll_user_agreement)
    LinearLayout llUserAgreement;
    @BindView(R.id.ll_private_agreement)
    LinearLayout llPrivateAgreement;
    @BindView(R.id.ll_about_change)
    LinearLayout llAboutChange;
    @BindView(R.id.tv_logout)
    TextView tvLogout;

    public static void actionStar(Context context){
        Intent intent = new Intent(context,SetActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_set;
    }

    @Override
    protected void initView() {
        setTitleText("设置");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }


    @OnClick({R.id.ll_bind_account, R.id.ll_black_list, R.id.ll_delete_account, R.id.ll_private_set, R.id.ll_check_version, R.id.ll_user_agreement, R.id.ll_private_agreement, R.id.ll_about_change, R.id.tv_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_bind_account:
                BindAccountActivity.actionStar(context);
                break;
            case R.id.ll_black_list:
                break;
            case R.id.ll_delete_account:
                remindDestroy();
                break;
            case R.id.ll_private_set:
                break;
            case R.id.ll_check_version:
                break;
            case R.id.ll_user_agreement:
                break;
            case R.id.ll_private_agreement:
                break;
            case R.id.ll_about_change:
                break;
            case R.id.tv_logout:
                logout();
                break;
        }
    }

    private void remindDestroy() {
        WarningDialog dialog = new WarningDialog(context,"注销后将无法登录并删除所有信息","取消","确认","确定注销账号？");
        dialog.setClicklistener(new WarningDialog.ClickListenerInterface() {
            @Override
            public void doCancel() {

            }

            @Override
            public void doConfirm() {
                destroyAccount();
            }
        });
        dialog.show();
    }

    private void destroyAccount() {
        OkGoUtil.postRequest(getContext(), UrlConstants.DESTROY_ACCOUNT, null, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        logout();
                    }
                    ToastUtil.showToast(context,response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void logout() {
        SPUtils.setBooleanData(context, Constants.IS_LOGIN,false);
        ActivityCollector.logout(context);
        Intent intent = new Intent(context, OneKeyLoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}